package com.zeei.bfs.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zeei.bfs.dao.SysLogDao;
import com.zeei.bfs.service.RoleService;
import com.zeei.bfs.vo.QueryRoleParamVO;
import com.zeei.bfs.vo.RoleVO;
import com.zeei.common.annotation.SysLog;
import com.zeei.common.controller.BasicController;
import com.zeei.common.enums.LogType;
import com.zeei.common.utils.SessionInfoUtil;
import com.zeei.common.utils.UserOperationLog;
import com.zeei.common.vo.ResultBasicVO;
import com.zeei.common.vo.ResultRowsVO;
import com.zeei.common.vo.UserVO;

@Controller
@RequestMapping("/bfs/role")
public class RoleController extends BasicController {

	@Resource
	private RoleService roleService;
	
	@Resource
	private SysLogDao syslogDao;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<RoleVO> queryRole(QueryRoleParamVO queryRoleParam) {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		PageInfo<RoleVO> rolePage = roleService.queryRole(queryRoleParam, user);
		return this.createResultRowsVO(rolePage.getList(), true, null, (int) rolePage.getTotal());
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "增加角色", type = LogType.Edit)
	public ResultBasicVO addRole(RoleVO role) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 增加"+role.getRoleName()+"的角色 操作.", LogType.Add));
		Integer userId = SessionInfoUtil.getSessionUserId();
		role.setCreateId(userId);
		return this.createResultBasicVO(roleService.addRole(role), null);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "编辑角色", type = LogType.Edit)
	public ResultBasicVO editRole(RoleVO role) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 编辑"+role.getRoleName()+"的角色 操作.", LogType.Edit));
		return this.createResultBasicVO(roleService.editRole(role), null);
	}

	@RequestMapping(value = "/drop", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "删除角色", type = LogType.Edit)
	public ResultBasicVO dropRole(Integer[] roleIds) {
		List<Integer> list = Arrays.asList(roleIds);
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 删除ID为"+StringUtils.join(roleIds, ",")+"的角色 操作.", LogType.Delete));
		return this.createResultBasicVO(roleService.dropRole(list), null);
	}

	@RequestMapping(value = "/valid", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validRoleName(RoleVO role) {
		return this.createResultBasicVO(roleService.validRoleName(role), null);
	}

	@RequestMapping(value = "/combobox", method = RequestMethod.POST)
	@ResponseBody
	public List<RoleVO> queryComboboxRole(String userId) {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		return roleService.queryAllRole(user, userId);
	}

}
